package edu.pku.sei.modeler.uml.commands.timing;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.requests.CreationFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

import edu.pku.sei.gefcommon.descriptor.views.PopupPropertySheetDialog;
import edu.pku.sei.modeler.exceptions.FactoryNotFoundException;
import edu.pku.sei.modeler.model.common.ModelRegistry;
import edu.pku.sei.modeler.model.core.MetaModel;
import edu.pku.sei.modeler.model.structure.MetaDiagram;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.uml.BasicInteractions.StateInvariant;
import edu.pku.sei.modeler.model.uml.temp.timeline.TimingLifeLine;
import edu.pku.sei.modeler.uml.commands.UMLElementCreateCommand;
import edu.pku.sei.modeler.uml.modelHelper.ModelPackage;

public class TimingLifeLineCreateCommand extends UMLElementCreateCommand {

	public TimingLifeLineCreateCommand(MetaNode node, MetaDiagram diagram,
			Rectangle constraint) {
		super(node, diagram, constraint);
	}
	
	@Override
	protected void updateModelElement() {
		
		PopupPropertySheetDialog dlg = new PopupPropertySheetDialog(new Shell(PlatformUI.getWorkbench().getActiveWorkbenchWindow()
				.getActivePage().getWorkbenchWindow().getShell(), SWT.DIALOG_TRIM | SWT.RESIZE | SWT.SYSTEM_MODAL)){
			protected Control createDialogArea(Composite parent) {
				Control control = super.createDialogArea(parent);
				getShell().setText("add states");
				return control;
			}
		};
		dlg.setListInputValue(((TimingLifeLine)node.getModelElement()).getIncludedStateInvariants(), new Class[]{StateInvariant.class});
		final MetaModel model = node.getModel();
		dlg.setCreationFactory(new CreationFactory() {
			public Object getNewObject() {
				try {
					return ModelRegistry.getRegistry().getFactory(model).createElement(ModelPackage.__STATEINVARIANT__, true);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (FactoryNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return null;
			}
			public Object getObjectType() {
				return null;
			}
		}
			
		);
//		dlg.getShell().setText("add states");
		dlg.open();
		super.updateModelElement();
	}
	
}
